package com.px.auditRecord.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import com.px.auditRecord.domain.AuditRecordDrone;
import com.px.auditRecord.domain.ForRecordUser;
import com.px.auditRecord.service.IAuditRecordDroneService;
import com.px.auditRecord.service.IForRecordUserService;
import com.px.auditRecord.vo.BlackWhiteDroneVo;
import com.px.system.domain.FlightUser;
import com.px.system.service.IFlightUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.px.auditRecord.mapper.AuditBlackWhiteMapper;
import com.px.auditRecord.domain.AuditBlackWhite;
import com.px.auditRecord.service.IAuditBlackWhiteService;

/**
 * 黑白名单控制Service业务层处理
 * 
 * @author WRJ
 * @date 2024-08
 */
@Service
public class AuditBlackWhiteServiceImpl implements IAuditBlackWhiteService 
{
    @Autowired
    private AuditBlackWhiteMapper auditBlackWhiteMapper;
    @Autowired
    private IForRecordUserService forRecordUserService;
    @Autowired
    private IFlightUserService flightUserService;
    @Autowired
    private IAuditRecordDroneService auditRecordDroneService;

    /**
     * 查询黑白名单控制
     * 
     * @param id 黑白名单控制主键
     * @return 黑白名单控制
     */
    @Override
    public AuditBlackWhite selectAuditBlackWhiteById(Long id)
    {
        return auditBlackWhiteMapper.selectAuditBlackWhiteById(id);
    }

    /**
     * 查询黑白名单控制列表
     * 
     * @param auditBlackWhite 黑白名单控制
     * @return 黑白名单控制
     */
    @Override
    public List<AuditBlackWhite> selectAuditBlackWhiteList(AuditBlackWhite auditBlackWhite)
    {
        return auditBlackWhiteMapper.selectAuditBlackWhiteList(auditBlackWhite);
    }

    /**
     * 新增黑白名单控制
     * 
     * @param auditBlackWhite 黑白名单控制
     * @return 结果
     */
    @Override
    public int insertAuditBlackWhite(AuditBlackWhite auditBlackWhite)
    {
        // 人员控制
        if ("1".equals(auditBlackWhite.getControlType())) {
            // 获取身份证下所有账号,并删除这些规则
            List<String> phone = auditBlackWhiteMapper.selectFlightPhoneByIdcard(auditBlackWhite.getIdCard());
            if (CollectionUtil.isNotEmpty(phone)) {
                phone.forEach(this::deleteByPhone);
            }
            ForRecordUser forRecordUser = forRecordUserService.selectForRecordUserByIdcard(auditBlackWhite.getIdCard());
            auditBlackWhite.setName(forRecordUser.getName());
            auditBlackWhite.setPhoneNumber(forRecordUser.getPhonenumber());
            auditBlackWhite.setUserName(forRecordUser.getPhonenumber());
        }
        // 账号控制
        if ("2".equals(auditBlackWhite.getControlType())) {
            // 获取手机号下所有设备,并删除这些规则
            deleteByPhone(auditBlackWhite.getPhoneNumber());

            FlightUser flightUser = new FlightUser();
            flightUser.setPhonenumber(auditBlackWhite.getPhoneNumber());

            String idCard = flightUserService.selectIdCardByPhone(auditBlackWhite.getPhoneNumber());
            auditBlackWhite.setIdCard(idCard);
            ForRecordUser forRecordUser = forRecordUserService.selectForRecordUserByIdcard(idCard);
            auditBlackWhite.setName(forRecordUser.getName());
            List<FlightUser> flightUserList = flightUserService.selectFlightUserList(flightUser);
            if (CollectionUtil.isNotEmpty(flightUserList)) {
                auditBlackWhite.setUserName(flightUserList.get(0).getUserName());
            }
        }
        // 设备控制
        if ("3".equals(auditBlackWhite.getControlType())) {
            // 如果此设备之前存在控制记录,先删除后新增
            deleteBySn(auditBlackWhite.getDroneSn());

            AuditRecordDrone auditRecordDrone = new AuditRecordDrone();
            auditRecordDrone.setSn(auditBlackWhite.getDroneSn());
            List<AuditRecordDrone> auditRecordDrones = new ArrayList<>();
            try {
                auditRecordDrones = auditRecordDroneService.selectAuditRecordDroneList(auditRecordDrone);
            } catch (Exception e) {}

            if (CollectionUtil.isNotEmpty(auditRecordDrones)) {
                AuditRecordDrone recordDrone = auditRecordDrones.get(0);
                FlightUser flightUser = flightUserService.selectFlightUserByUserId(NumberUtil.parseLong(recordDrone.getCreateBy()));
                ForRecordUser forRecordUser = forRecordUserService.selectForRecordUserByIdcard(recordDrone.getIdcard());
                auditBlackWhite.setName(forRecordUser.getName());
                auditBlackWhite.setUserName(flightUser.getUserName());
                auditBlackWhite.setPhoneNumber(flightUser.getPhonenumber());
                auditBlackWhite.setIdCard(flightUserService.selectIdCardByPhone(flightUser.getPhonenumber()));
            }
        }

        return auditBlackWhiteMapper.insertAuditBlackWhite(auditBlackWhite);
    }

    private void deleteByPhone(String phone) {
        AuditBlackWhite phoneBlackWhite = new AuditBlackWhite();
        phoneBlackWhite.setPhoneNumber(phone);
        phoneBlackWhite.setControlType("2");
        List<AuditBlackWhite> auditBlackWhites = selectAuditBlackWhiteList(phoneBlackWhite);
        auditBlackWhites.forEach(i->deleteAuditBlackWhiteById(i.getId()));
        List<String> sn = auditBlackWhiteMapper.selectSnByPhone(phone);
        if (CollectionUtil.isNotEmpty(sn)) {
            sn.forEach(this::deleteBySn);
        }
    }

    private void deleteBySn(String sn) {
        AuditBlackWhite phoneBlackWhite = new AuditBlackWhite();
        phoneBlackWhite.setDroneSn(sn);
        phoneBlackWhite.setControlType("3");
        List<AuditBlackWhite> auditBlackWhites = selectAuditBlackWhiteList(phoneBlackWhite);
        auditBlackWhites.forEach(i->deleteAuditBlackWhiteById(i.getId()));
    }

    /**
     * 修改黑白名单控制
     * 
     * @param auditBlackWhite 黑白名单控制
     * @return 结果
     */
    @Override
    public int updateAuditBlackWhite(AuditBlackWhite auditBlackWhite)
    {
        return auditBlackWhiteMapper.updateAuditBlackWhite(auditBlackWhite);
    }

    /**
     * 批量删除黑白名单控制
     * 
     * @param ids 需要删除的黑白名单控制主键
     * @return 结果
     */
    @Override
    public int deleteAuditBlackWhiteByIds(Long[] ids)
    {
        return auditBlackWhiteMapper.deleteAuditBlackWhiteByIds(ids);
    }

    /**
     * 删除黑白名单控制信息
     * 
     * @param id 黑白名单控制主键
     * @return 结果
     */
    @Override
    public int deleteAuditBlackWhiteById(Long id)
    {
        return auditBlackWhiteMapper.deleteAuditBlackWhiteById(id);
    }

    @Override
    public List<BlackWhiteDroneVo> selectForRecordDroneList(BlackWhiteDroneVo droneVo) {
        return auditBlackWhiteMapper.selectForRecordDroneList(droneVo);
    }

    @Override
    public List<?> selectAccountUserListMap(FlightUser flightUser) {
        return auditBlackWhiteMapper.selectAccountUserListMap(flightUser);
    }

    @Override
    public List<AuditRecordDrone> selectDroneList(AuditRecordDrone auditRecordDrone) {
        return auditBlackWhiteMapper.selectDroneList(auditRecordDrone);
    }

    @Override
    public List<?> selectFlightUserListMap(FlightUser flightUser) {
        return auditBlackWhiteMapper.selectFlightUserListMap(flightUser);
    }
}
